package me.numark.fragments;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import me.numark.R;

/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * to handle interaction events.
 * Use the {@link InputMobileFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class InputMobileFragment extends Fragment implements View.OnClickListener {

    private RegisterListener mListener;

    private TextView rightButton;

    private EditText mobileEdit;

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @return A new instance of fragment InputMobileFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static InputMobileFragment newInstance() {
        InputMobileFragment fragment = new InputMobileFragment();
        return fragment;
    }

    public InputMobileFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_input_mobile, container, false);
        mobileEdit = (EditText) view.findViewById(R.id.input_mobile);
        mobileEdit.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s != null && s.length() == getResources().getInteger(R.integer.mobile_length)) {
                    rightButton.setEnabled(true);
                } else {
                    rightButton.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
        TextView title = (TextView) view.findViewById(R.id.title);
        title.setText(R.string.register);
        view.findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().onBackPressed();
            }
        });
        rightButton = (TextView) view.findViewById(R.id.right_text);
        rightButton.setEnabled(false);
        rightButton.setOnClickListener(this);
        rightButton.setText(R.string.next_step);
        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mListener = (RegisterListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    @Override
    public void onClick(View v) {
        Bundle args = new Bundle();
        args.putString("mobile", mobileEdit.getText().toString());
        mListener.onNext(this, args);
    }


}